package com.scada.service.impl;

import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scada.dao.IRoleDao;
import com.scada.inf.RoleVisitor;
import com.scada.model.bean.Role;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;

@Service("roleVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class RoleVisitorImpl implements RoleVisitor{

	@Autowired
	private IRoleDao iRoleDao;
	
	@Override
	public PageInfo<Role> selectDataList(PageInfo<Role> pageInfo) {
		Role role = (Role)pageInfo.getCondition();
		int count = iRoleDao.count(role);
		pageInfo.setRecordsTotal(count);
		List<Role> rli = iRoleDao.selectDataList(pageInfo);
		pageInfo.setData(rli);
		return pageInfo;
	}
	
	@Override
	public ResponseBean selectAllDataList(){
		ResponseBean rb = new ResponseBean();
		try{
			rb.addObject("roleList", iRoleDao.selectAllDataList());
			rb.success();
		}catch(Exception e){
			e.printStackTrace();
		}
		return rb;
	}

	@Override
	public ResponseBean selectCodeExist(String sysRoleCode) {
		ResponseBean rb = new ResponseBean();
		try{
			Integer size = iRoleDao.selectCodeExist(sysRoleCode);
			if(size == 0){
				rb.addObject("exist", false);
			}else{
				rb.addObject("exist", true);
			}
			rb.success();
		}catch(Exception e){
			e.printStackTrace();
		}
		return rb;
	}

	@Override
	public ResponseBean insert(Role role) {
		ResponseBean rb = new ResponseBean();
		try{
			Integer size = iRoleDao.insert(role);
			if(size == 0){
				rb.failed();
			}else{
				rb.success();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return rb;
	}

	@Override
	public ResponseBean updateByPrimaryKey(Role role) {
		ResponseBean rb = new ResponseBean();
		try{
			Integer size = iRoleDao.updateByPrimaryKey(role);
			if(size == 0){
				rb.failed();
			}else{
				rb.success();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return rb;
	}

	@Override
	public ResponseBean deleteByPrimaryKey(Map<String, String> map) {
		ResponseBean rb = new ResponseBean();
		try{
			Integer size = iRoleDao.deleteByPrimaryKey(map);
			if(size == 0){
				rb.failed();
			}else{
				rb.success();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return rb;
	}

	@Override
	public ResponseBean selectRoleByPrimaryKey(Integer id) {
		ResponseBean rb = new ResponseBean();
		try{
			Role role = iRoleDao.selectRoleByPrimaryKey(id);
			rb.success();
			rb.addObject("role", role);
		}catch(Exception e){
			e.printStackTrace();
		}
		return rb;
	}

	@Override
	public ResponseBean selectRoleByCode(String sysRoleCode) {
		ResponseBean rb = new ResponseBean();
		try{
			Role role = iRoleDao.selectRoleByCode(sysRoleCode);
			rb.success();
			rb.addObject("role", role);
		}catch(Exception e){
			e.printStackTrace();
		}
		return rb;
	}

	@Override
	public List<Role> getAllRolesName(Integer userId) {
		
		return iRoleDao.getAllRolesName(userId);
	}

}
